package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.common.CustomIdGenerator;
import com.itheima.pinda.entity.AddressBook;
import com.itheima.pinda.mapper.PdAddressBookMapper;
import com.itheima.pinda.service.PdAddressBookService;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 地址簿服务类实现
 */
@Service
public class PdAddressBookServiceImpl implements PdAddressBookService {

    @Autowired
    private CustomIdGenerator idGenerator;
    @Autowired
    private PdAddressBookMapper addressBookMapper;
    @Autowired
    private CacheChannel cacheChannel;

    private String region = "addressBook";

    @Override
    @Transactional
    public AddressBook insert(AddressBook addressBook) {
        // 判断是否是默认地址
        if (1 == addressBook.getIsDefault()) {
            addressBookMapper.update(null,
                    new LambdaUpdateWrapper<AddressBook>()
                            .set(AddressBook::getIsDefault,0)
                            .eq(AddressBook::getUserId,addressBook.getUserId()));
        }
        if (StringUtils.isEmpty(addressBook.getId())) {
            // 新增
            addressBook.setId(idGenerator.nextId(addressBook) + "");
            addressBookMapper.insert(addressBook);
        }else {
            // 修改
            addressBookMapper.updateById(addressBook);
            cacheChannel.evict(region, addressBook.getId());
        }

        cacheChannel.set(region, addressBook.getId(), addressBook);

        return addressBook;
    }

    @Override
    public AddressBook selectById(String id) {
        // 先查询缓存
        CacheObject cacheObject = cacheChannel.get(region, id);

        if (ObjectUtils.isEmpty(cacheObject.getValue())) {
            AddressBook addressBook = addressBookMapper.selectById(id);
            cacheChannel.set(region, addressBook.getId(), addressBook);
            return addressBook;
        }

        return (AddressBook) cacheObject.getValue();
    }

    @Override
    public IPage selectByPage(Integer pageNum, Integer pageSize, String userId, String keyword) {

        Page<AddressBook> page = new Page<>(pageNum,pageSize);
        Page<AddressBook> selectPage = addressBookMapper.selectPage(page,
                new LambdaQueryWrapper<AddressBook>()
                        .eq(!StringUtils.isEmpty(userId), AddressBook::getUserId, userId)
                        .eq(!StringUtils.isEmpty(keyword), AddressBook::getAddress, keyword));

        return selectPage;
    }

    @Override
    public void delete(List<String> ids) {
        // 清除缓存
        ids.forEach(id->{
            cacheChannel.evict(region,id);
        });

        addressBookMapper.delete(new LambdaQueryWrapper<AddressBook>().in(AddressBook::getId,ids));

    }
}